"use strict";

var $ = require("jquery");

const tapEvent = "click";

class ButtonsInternal {
    constructor(core, puzzle, dashboard) {
        this.core = core;
        this.puzzle = $(puzzle);
        this.dashboard = $(dashboard);
    }

    bindEvents() {
        var _this = this;
        this.dashboard.on(tapEvent, "button", function(e) {
            var id = $(e.currentTarget).prop("id");
            console.log("id", id);
            if (typeof _this[id] === "function") {
                _this[id]();
            } else {
                console.error(`cannot found id[${id}]`);
            }
        });
    }

    check() {
        if (this.core.check()) {
            alert("成功");
        } else {
            alert("失败");
        }
    }

    reset() {
        $("span:not(.fixed)", this.puzzle).text(0)
            .removeClass("mark1 mark2 error")
            .addClass("empty");
    }

    clear() {
        $("span.error", this.puzzle).removeClass("error");
    }

    rebuild() {
        this.core.rebuild();
    }
}

const __ = "__";

class Buttons {
    constructor(core, puzzle, dashboard) {
        this[__] = new ButtonsInternal(core, puzzle, dashboard);
    }

    bindEvents() {
        this[__].bindEvents();
    }
}

module.exports = Buttons;
